var nodemailer = require('nodemailer')

// 1.创建一个Nodemailer smtp传输对象
let transporter = nodemailer.createTransport({
  host: 'smtp.qq.com',
  port: '587',
  secureConnection: true,
  secure: false, // true for 465, false for other ports
  auth: { user: '2419394343@qq.com', pass: 'xdcqziwmwwisecic' }
})

const MailController = {
  // 成绩发布提醒
  sendmail: async (req, res) => {
    // 邮件发送
    // 2. 配置消息
    var mailOptions = {
      from: '2419394343@qq.com', // sender address
      to: `${req.body.email}`, // list of receivers
      subject: '高校教学考试成绩管理系统', // Subject line
      text: `${req.body.name}同学你好，你所修课程 ${req.body.coursename}(课程编号：${req.body.courseid}) 的成绩已发布，请登录系统查询` // plaintext body
    }
    // 3. 发送
    transporter.sendMail(mailOptions, function (error, info) {
      if (!error) {
        return res.send('success')
      } else {
        return res.send('error')
      }
    })
  },

  // 撤销申请
  submitmail: async (req, res) => {
    // 2. 配置消息
    var mailOptions = {
      from: '2419394343@qq.com', // sender address
      to: `617947173@qq.com`, // 管理员的邮箱
      subject: '高校教学考试成绩管理系统', // Subject line
      text: `管理员您好，${req.body.name}老师向您提交 ${req.body.coursename}(课程编号：${req.body.courseid}) 的课程成绩撤销申请，请登录系统进行操作` // plaintext body
    }
    // 3. 发送
    transporter.sendMail(mailOptions, function (error, info) {
      if (!error) {
        return res.send('success')
      } else {
        return res.send('error')
      }
    })
  }
}

module.exports = MailController
